﻿// <copyright file="AdminDynamicNodeProvider.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Blog.Services.Infrastructure
{
    using MvcSiteMapProvider;
    using System;
    using System.Linq;
    using System.Collections.Generic;

    using Core.Infrastructure;

    public class AdminDynamicNodeProvider : DynamicNodeProviderBase
    {
        private BlogEntryService _blogEntryService;
        private readonly AppCache appCache;

        public AdminDynamicNodeProvider()
        {
            this.appCache = new AppCache();
        }

        public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
        {
            if (appCache.Exists("AdminBlogEntries"))
                return (List<DynamicNode>)appCache.Get("AdminBlogEntries");

            _blogEntryService = IoC.Get<BlogEntryService>();
            var nodes = new List<DynamicNode>();

            var entries = _blogEntryService.GetAll();
            foreach (var entry in entries)
            {
                var pageNode = new DynamicNode { Key = "entry_" + Guid.NewGuid() + "_" + entry.Id, Title = entry.Header };
                pageNode.RouteValues.Add("id", entry.Id);
                pageNode.Action = "Edit";
                pageNode.Controller = "Blog";
                pageNode.Area = "CleverCms.Admin";
                nodes.Add(pageNode);
            }

            //Edit categories 
            var categories = _blogEntryService.GetAllCategories();
            foreach (var category in categories)
            {
                var categoryNode = new DynamicNode
                {
                    Key = "category_" + Guid.NewGuid() + "_" + category.Id,
                    Title = category.CategoryName
                };
                categoryNode.RouteValues.Add("id", category.Id);
                categoryNode.Action = "EditCategory";
                categoryNode.Controller = "Blog";
                categoryNode.Area = "CleverCms.Admin";
                nodes.Add(categoryNode);
            }

            appCache.Add("AdminBlogEntries", nodes);
            return nodes;
        }
    }
}
